﻿using DevExpress.Data;
using DevExpress.Utils.Win;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Demos.util;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DevExpress.XtraReports.UI;
using DXApplicationTangche.Const;
using DXApplicationTangche.report;
using DXApplicationTangche.UC.成品出库单;
using DXApplicationTangche.UC.成品出库单.service;
using DXApplicationTangche.UC.成衣进销存.service;
using DXApplicationTangche.UC.抖音出入库;
using DXApplicationTangche.UC.抖音出入库.Service;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;
using DXApplicationTangche.UC.货号管理.service;
using DXApplicationTangche.UC.钉钉;
using DXApplicationTangche.UC.销售记录.service;
using DXApplicationTangche.UC.门店出入库.Service;
using DXApplicationTangche.UC.面料审核.Service;
using DXApplicationTangche.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static DXApplicationTangche.UC.成衣进销存.service.成衣进销存Service;

namespace DXApplicationTangche.UC.面料审核
{
    public partial class Form生产计划单入库 : DevExpress.XtraEditors.XtraForm
    {
        private String shopId { get; set; }

        private List<String> materialRequirementIds = new List<string>();
        private List<MaterialRequirementQueryDTO> materialRequirementQueryDTOs = new List<MaterialRequirementQueryDTO>();
        private List<String> barcodes = new List<string>();
        private List<BarcodeBaseInfo> barcodeBaseInfos = new List<BarcodeBaseInfo>();
        private StockInOutBatchDTO stockInOutBatchDTO出库 = new StockInOutBatchDTO();

        //回库
        private List<StockInOutDetailDTO> otherStockInOutDetailDTOs = new List<StockInOutDetailDTO>();
        //显示
        private List<StockShowDTO> stockShowDTOs显示 = new List<StockShowDTO>();

        public Form生产计划单入库()
        {
            InitializeComponent();
            //this.simpleButton打印出库单.Enabled = false;
            //this.simpleButton出库.Enabled = false;
            this.xtraTabControl2.SelectedTabPage = this.xtraTabPage批量出库;
        }

        private void textBox扫描成衣_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (!String.IsNullOrWhiteSpace(this.textBox扫描成衣.Text))
                {
                    try
                    {
                        //判断成衣码是否正确
                        if (!货号管理Service.barcodeExisted(this.textBox扫描成衣.Text.Trim()))
                        {
                            MessageBox.Show("成衣码不存在");
                            this.textBox扫描成衣.Clear();
                            return;
                        }
                        //  取出入库List<barcode>
                        //id为空，不可入库，直接返回
                        OrderCommitDTO返回 orderCommitDTO =
                                订单预览新Service.getOrderCommitDTOByBarcode(this.textBox扫描成衣.Text.Trim());
                        StockShowDTO stockShowDTO = new StockShowDTO(
                            this.textBox扫描成衣.Text.Trim(),
                            orderCommitDTO.styleNameCn,
                            orderCommitDTO.materialCode,
                            orderCommitDTO.styleSizeCd);
                        MaterialRequirementQueryDTO materialRequirementQueryDTO = 面料审核Service.getMaterialRequirementInfoByBarcode(this.textBox扫描成衣.Text);
                        //if (String.IsNullOrEmpty(materialRequirementQueryDTO.id))
                        //{
                        //    MessageBox.Show("成衣类型有误，不可批量出库,请前往工厂调拨tab页出库");
                        //    this.textBox扫描成衣.Clear();
                        //    return;
                        //}
                        ////扫码判断shopId是否一致
                        //if (this.materialRequirementQueryDTOs.Count >= 1)
                        //{
                        //    if (!this.shopId.Equals(materialRequirementQueryDTO.shopRightQueryFacadeDTO.shopId))
                        //    {
                        //        MessageBox.Show("扫描成衣业务类型不符，不可扫描");
                        //        return;
                        //    }
                        //    this.materialRequirementQueryDTOs.Add(materialRequirementQueryDTO);
                        //    //id判重，不包含添加到list
                        //    //barcode判重，不包含添加到list
                        //    if (!this.materialRequirementIds.Contains(materialRequirementQueryDTO.id))
                        //        this.materialRequirementIds.Add(materialRequirementQueryDTO.id);
                        //    if (!this.barcodes.Contains(this.textBox扫描成衣.Text))
                        //    {
                        //        this.barcodes.Add(this.textBox扫描成衣.Text);
                        //        this.barcodeBaseInfos.Add(materialRequirementQueryDTO.barcodeBaseInfo);
                        //    }
                        //    this.刷新();
                        //}
                        //else
                        //{
                        this.shopId = "14";
                        if (!this.barcodes.Contains(this.textBox扫描成衣.Text))
                        {
                            this.barcodes.Add(this.textBox扫描成衣.Text);
                            this.stockShowDTOs显示.Add(stockShowDTO);
                        }
                        this.materialRequirementQueryDTOs.Add(materialRequirementQueryDTO);
                        this.materialRequirementIds.Add(materialRequirementQueryDTO.id);
                        this.barcodeBaseInfos.Add(materialRequirementQueryDTO.barcodeBaseInfo);
                        this.刷新();
                        //}
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                else
                {
                    MessageBox.Show("请扫码");
                }
            }
        }

        private void 刷新()
        {
            this.gridControl扫码显示.DataSource = this.stockShowDTOs显示;
            this.gridView4.RefreshData();
            this.textBox扫描成衣.Clear();
        }

        private void simpleButton出库_Click(object sender, EventArgs e)
        {
            if (this.barcodes.Count() == 0)
            {
                MessageBox.Show("无成衣信息，不可出库");
                return;
            }
            MessageBoxButtons messBtn = MessageBoxButtons.OKCancel;
            DialogResult dr = MessageBox.Show("是否出库？", "提示:确认", messBtn);
            if (dr == DialogResult.OK)
            {
                //入库后停3秒再出库，加载转圈等待
                this.splashScreenManager.ShowWaitForm();
                this.splashScreenManager.SetWaitFormCaption("请稍后,正在加载中....");     // 标题
                this.splashScreenManager.SetWaitFormDescription("正在初始化.....");     // 信息
                try 
                {
                    //调用入库接口
                    //StockInOutBatchDTO stockInOutBatchDTO = 面料审核Service.REsaveBarcoderStockHandle(1, "抖音入库", this.shopId, this.barcodes);
                    //线程停3秒
                    //Thread.Sleep(3000);
                    //调用出库接口
                    this.stockInOutBatchDTO出库 = 面料审核Service.REsaveBarcoderStockHandle(0, "抖音出库", this.shopId, this.barcodes);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("出库错误，请检查条码是否正确");
                    return;
                }
                //出库按钮关闭
                this.splashScreenManager.CloseWaitForm();
                this.出库后跳转();
            }
            //出库后清空
            this.barcodes = null;
            this.stockShowDTOs显示 = null;
            this.刷新();
        }
        private void 出库后跳转()
        {
            if (this.stockInOutBatchDTO出库.stockInOutDetailDTOS.Count() == 0)
            {
                MessageBox.Show("无出库信息，不可出库");
                return;
            }
            //出库单title
            StockInOutTitleDTO 出库单DTO = new StockInOutTitleDTO(
                                        this.stockInOutBatchDTO出库.inOutCode,
                                        DateTime.Now,
                                        this.stockInOutBatchDTO出库.stockInOutDetailDTOS[0].inOutFlag.ToString(),
                                        this.stockInOutBatchDTO出库.stockInOutDetailDTOS[0].inOutType,
                                        "fengjinyue01",
                                        "冯金月",
                                        this.stockInOutBatchDTO出库.stockInOutDetailDTOS.Count(),
                                        this.stockInOutBatchDTO出库.stockInOutDetailDTOS[0].storeId,
                                        this.stockInOutBatchDTO出库.stockInOutDetailDTOS[0].storeName,
                                        ""
                                    );
            //出库单report
            foreach (StockInOutDetailDTO stockInOutDetailDTO in this.stockInOutBatchDTO出库.stockInOutDetailDTOS)
            {
                try { stockInOutDetailDTO.styleSizeCd = stockInOutDetailDTO.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
            }
            DXApplicationTangche.UC.成品出库单.service.出库单导出DTO 出库单导出DTO = new DXApplicationTangche.UC.成品出库单.service.出库单导出DTO(出库单DTO, this.stockInOutBatchDTO出库.stockInOutDetailDTOS);
            XtraReport出库单 report = new XtraReport出库单(出库单导出DTO);
            //出库单时间
            String stockTime = DateTime.Now.ToString();
            try { stockTime = stockTime.Replace(" ", "").Replace("/", "").Replace(":", ""); } catch { }
            String id = "douyinStockOut_" + this.stockInOutBatchDTO出库.inOutCode;
            //钉钉
            try
            {
                //  上传pdf:
                string saveDirectoryPath = @"pdf\";
                //  1.先本地生成文件
                report.ExportToPdf(saveDirectoryPath + id + @".pdf");
                //  2.上传本地文件
                String re = 成品出库单Service.uploadStockInOutFileByCode(id, saveDirectoryPath + id + @".pdf");
                //  3.删除本地文件
                //File.Delete(saveDirectoryPath + re);
                //  钉钉
                钉钉Service.start(new ProcessStartDTO(调拨出库类型Consts.抖音出库, 出库单DTO, re).build("17866554337", 409162359L));
            }
            catch (Exception ex)
            {
                MessageBox.Show("服务连接失败，钉钉发送失败，请联系李金键");
            }
            //ReportPrintTool tool = new ReportPrintTool(report);
            //tool.ShowPreview();

            //显示
            //弹框抖音发货出库对照
            new Form抖音发货出库对照().ShowDialog();
        }
        private void simpleButton打印出库单_Click(object sender, EventArgs e)
        {
            if (this.stockInOutBatchDTO出库.stockInOutDetailDTOS.Count == 0)
            {
                MessageBox.Show("无出库信息，不可出库");
                return;
            }
            //出库单title
            StockInOutTitleDTO 出库单DTO = new StockInOutTitleDTO(
                                        this.stockInOutBatchDTO出库.inOutCode,
                                        DateTime.Now,
                                        this.stockInOutBatchDTO出库.stockInOutDetailDTOS[0].inOutFlag.ToString(),
                                        this.stockInOutBatchDTO出库.stockInOutDetailDTOS[0].inOutType,
                                        "fengjinyue01",
                                        "冯金月",
                                        this.stockInOutBatchDTO出库.stockInOutDetailDTOS.Count(),
                                        this.stockInOutBatchDTO出库.stockInOutDetailDTOS[0].storeId,
                                        this.stockInOutBatchDTO出库.stockInOutDetailDTOS[0].storeName,
                                        ""
                                    );
            //出库单report
            foreach(StockInOutDetailDTO stockInOutDetailDTO in this.stockInOutBatchDTO出库.stockInOutDetailDTOS)
            {
                try { stockInOutDetailDTO.styleSizeCd = stockInOutDetailDTO.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
            }
            DXApplicationTangche.UC.成品出库单.service.出库单导出DTO 出库单导出DTO = new DXApplicationTangche.UC.成品出库单.service.出库单导出DTO(出库单DTO, this.stockInOutBatchDTO出库.stockInOutDetailDTOS);
            XtraReport出库单 report = new XtraReport出库单(出库单导出DTO);
            //出库单时间
            String stockTime = DateTime.Now.ToString();
            try { stockTime = stockTime.Replace(" ", "").Replace("/", "").Replace(":", ""); } catch { }
            String id = "douyinStockOut_" + this.stockInOutBatchDTO出库.inOutCode;
            //钉钉
            try
            {
                //  上传pdf:
                string saveDirectoryPath = @"pdf\";
                //  1.先本地生成文件
                report.ExportToPdf(saveDirectoryPath + id + @".pdf");
                //  2.上传本地文件
                String re = 成品出库单Service.uploadStockInOutFileByCode(id, saveDirectoryPath + id + @".pdf");
                //  3.删除本地文件
                //File.Delete(saveDirectoryPath + re);
                //  钉钉
                钉钉Service.start(new ProcessStartDTO(调拨出库类型Consts.抖音出库, 出库单DTO, re).build("17866554337", 409162359L));
            }
            catch (Exception ex)
            {
                MessageBox.Show("服务连接失败，钉钉发送失败，请联系李金键");
            }
            //ReportPrintTool tool = new ReportPrintTool(report);
            //tool.ShowPreview();

            //显示
            //弹框抖音发货出库对照
            new Form抖音发货出库对照().ShowDialog();
            //this.mrpStockOutDetailDTOs = 面料审核Service.getKingeeStockInventoryByFnames(this.materialRequirementIds);
            //this.gridControl入库明细.DataSource = this.mrpStockOutDetailDTOs;
            //this.bandedGridView入库明细.ExpandAllGroups();
            //this.bandedGridView入库明细.RefreshData();
            //this.bandedGridView入库明细.GroupSummary.AddRange(new GridSummaryItem[] { new GridGroupSummaryItem(SummaryItemType.Count, "barcode", null, "(成衣数量总计= {0})"), });
            //this.bandedGridView入库明细.GroupSummary.AddRange(new GridSummaryItem[] { new GridGroupSummaryItem(SummaryItemType.Sum, "price", null, "(金额总计= {0})"), });
            //this.xtraTabControl1.SelectedTabPage = this.xtraTabPage成衣入库明细;

            ////提交出库
            //new Form断料裁剪提交().ShowDialog();
            //SaveFileDialog fileDialog = new SaveFileDialog();
            //fileDialog.Title = "导出Excel";
            //fileDialog.Filter = "Excel文件(*.xlsx)|*.xlsx";
            //DialogResult dialogResult = fileDialog.ShowDialog(this);
            //if (dialogResult == DialogResult.OK)
            //{
            //    //传materialIds
            //    if (this.materialRequirementIds.Count == 0)
            //    {
            //        MessageBox.Show("无扫描信息，不可提交");
            //        return;
            //    }
            //    //tab页一览显示
            //    this.mrpStockOutDetailDTOs = 面料审核Service.getKingeeStockInventoryByFnames(this.materialRequirementIds);
            //    this.gridControl入库明细.DataSource = this.mrpStockOutDetailDTOs;
            //    this.bandedGridView入库明细.ExpandAllGroups();
            //    this.bandedGridView入库明细.RefreshData();
            //    this.bandedGridView入库明细.GroupSummary.AddRange(new GridSummaryItem[] { new GridGroupSummaryItem(SummaryItemType.Count, "barcode", null, "(成衣数量总计= {0})"), });
            //    this.bandedGridView入库明细.GroupSummary.AddRange(new GridSummaryItem[] { new GridGroupSummaryItem(SummaryItemType.Sum, "price", null, "(金额总计= {0})"), });
            //    this.xtraTabControl1.SelectedTabPage = this.xtraTabPage成衣入库明细;

            //    List<RetailStockOutDTO> retailStockOutDTOs = 抖音Service.getRetailStockOutByBarcodes(this.barcodes);
            //    foreach (RetailStockOutDTO retailStockOutDTO in retailStockOutDTOs)
            //    {
            //        retailStockOutDTO.styleSizeCd = retailStockOutDTO.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", "");
            //    }
            //    DataTable dt = FileUtil.DouyinDataTable(retailStockOutDTOs);
            //    String filePath = @"tag\" + "抖音出库单.xlsx";
            //    DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
            //    FileUtil.ToDouyinExcelView(dt, fileDialog.FileName, filePath, 3, 5, retailStockOutDTOs.Count(), 1);

            //    try
            //    {
            //        FileUtil.ToDouyinExcelView(dt, fileDialog.FileName, filePath, 3, 5, retailStockOutDTOs.Count(), 1);
            //        DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    }
            //    catch
            //    {
            //        DevExpress.XtraEditors.XtraMessageBox.Show("保存失败，文件没有权限或者被占用！", "提示", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Information);
            //    }
            //}
        }

        private void gridView4_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.Name.Equals("gridColumn尺码2"))
            {
                if (!String.IsNullOrWhiteSpace(e.DisplayText))
                {
                    try { e.DisplayText = e.DisplayText.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                }
                else
                {
                    e.DisplayText = "样衣无尺寸定义";
                }
            }
        }

        private void textBox回库扫描成衣码_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                StockInOutDetailDTO stockInOutDetailDTO = 出入库Service.otherStockIn(this.textBox回库扫描成衣码.Text.Trim(), "14");
                this.otherStockInOutDetailDTOs.Add(stockInOutDetailDTO);
                this.gridControl门店其他入库.DataSource = this.otherStockInOutDetailDTOs;
                this.gridView门店其他入库.RefreshData();
                this.gridView门店其他入库.BestFitColumns();
                this.textBox回库扫描成衣码.Clear();
            }
        }
        class StockShowDTO
        {
            public String barcode { get; set; }
            public String styleNameCn { get; set; }
            public String materialCode { get; set; }
            public String styleSizeCd { get; set; }
            public StockShowDTO()
            {

            }

            public StockShowDTO(string barcode, string styleNameCn, string materialCode, string styleSizeCd)
            {
                this.barcode = barcode;
                this.styleNameCn = styleNameCn;
                this.materialCode = materialCode;
                this.styleSizeCd = styleSizeCd;
            }
        }
    }
}
